<?php

namespace App\Http\Controllers\order;

use App\Http\Controllers\Controller;
use App\Jobs\orderCallbackNotify;
use App\Models\Mysql\XnMerchant;
use App\Models\Mysql\XnMerchantBill;
use App\Models\Mysql\XnMerchantGoodsInvoicing;
use App\Models\Mysql\XnOrder;
use App\Models\Mysql\XnOrderBill;
use App\Models\Mysql\XnSupplierGoods;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;


class OrderNotifyController extends Controller
{
    /**
     * @param int $goods_belong
     * @param string $order_sn
     * @param bool $result
     * @param string $order_sn_key
     * @return void
     */
    public function next(int $goods_belong, string $order_sn, bool $result, string $order_sn_key = 'order_sn'): void
    {
        try {
            /** @var XnOrder|null $order  **/
            $order = XnOrder::query()->where($order_sn_key, $order_sn)->first();
            if (!$order) {
                throw new Exception("订单不存在");
            }
            // 判断状态
            if (in_array($order->recharge_status, [1, 2])) {
                throw new Exception(sprintf("订单已经完成[%s]", $order->recharge_status == 1?'成功':'失败'));
            }
            orderCallbackNotify::dispatch($order,$result,request()->get('db', 'supplier'))->onQueue('merchant_notify');

        } catch (Throwable $e) {
            Log::channel('notify_info')->info($e->getMessage());
        }
    }


    #[Scene(scene: "yipai")]
    public function yipai(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('userOrderId');
        // 下一步
        $this->next(1, $order_sn, $status == 1);
        // 返回
        return '<receive>ok</receive>';
    }

    #[Scene(scene: "kameng")]
    public function kameng(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('outer_order_id');
        // 下一步
        if (in_array($status, ['200', '500'])) {
            $this->next(2, $order_sn, $status == '200');
        }
        // 返回
        echo'ok';
    }

    #[Scene(scene: "jiaoyiba")]
    public function jiaoyiba(Request $request)
    {
        $data = $request->sceneValidate('jiaoyibaJson', json2array($request->input('data')));

        $status = $data['orderState'];
        $order_sn = $data['merOrderNo'];
        // 下一步
        if (in_array($status, ['23', '24'])) {
            $this->next(3, $order_sn, $status == '24');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "fulu")]
    public function fulu(Request $request)
    {
        $status = $request->input('order_status');
        $order_sn = $request->input('customer_order_no');
        // 下一步
        if (in_array($status, ['success', 'failed'])) {
            $this->next(4, $order_sn, $status == 'success');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "shuoruan")]
    public function shuoruan(Request $request)
    {
        $status = match ($request->input('Orderstatu_int')) {
            default     => 0,
            16          => 1,
            20,21,26    => 2,
        };
        $order_sn = $request->input('Orderid');
        // 下一步
        $this->next(5, $order_sn, $status == 1);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "fenghuangshuka")]
    public function fenghuangshuka(Request $request)
    {
        $status = $request->input('resultno');
        $order_sn = $request->input('sporderid');
        // 下一步
        if (in_array($status, ['1', '9'])) {
            $this->next(6, $order_sn, $status == '1');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "chengtianxia")]
    public function chengtianxia(Request $request)
    {
        $status = $request->input('result');
        $order_sn = $request->input('ordernum');
        // 下一步
        if (in_array($status, ['success', 'fail'])) {
            $this->next(7, $order_sn, $status == 'success');
        }
        // 返回
        return $this->response->raw('OK');
    }

    #[Scene(scene: "chengjuan")]
    public function chengjuan(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('order_no');
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAILURE'])) {
            $this->next(8, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "yanhao")]
    public function yanhao(Request $request)
    {
        $status = $request->input('Status');
        $order_sn = $request->input('UserOrderId');
        // 下一步
        if (in_array($status, [2, 3])) {
            $this->next(9, $order_sn, $status == 2);
        }
        // 返回
        return $this->response->json(['code' => 0]);
    }

    #[Scene(scene: "lansexiongdi")]
    public function lansexiongdi(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('outTradeNo');
        // 下一步
        if (in_array($status, ['1', '3'])) {
            $this->next(10, $order_sn, $status == '1');
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "dashang")]
    public function dashang(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('downstreamSerialno');
        // 下一步
        if (in_array($status, ['02', '03'])) {
            $this->next(11, $order_sn, $status == '02');
        }
        // 返回
        return $this->response->raw('OK');
    }

    #[Scene(scene: "yunjin")]
    public function yunjin(Request $request)
    {
        $status = $request->input('orderStatus');
        $order_sn = $request->input('customerOrderNo');
        // 下一步
        if (in_array($status, ['success', 'failed'])) {
            $this->next(12, $order_sn, $status == 'success');
        }
        // 返回
        return $this->response->raw('OK');
    }

    #[Scene(scene: "zixuan")]
    public function zixuan(Request $request)
    {
        $status = $request->input('orderStatus');
        $order_sn = $request->input('outOrderId');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(13, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "yushang")]
    public function yushang(Request $request)
    {
        $status = $request->input('State');
        $order_sn = $request->input('MOrderID');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(14, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->json(['Code' => 0]);
    }

    #[Scene(scene: "weiyunka")]
    public function weiyunka(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(15, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "shangmengwang")]
    public function shangmengwang(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('externalOrderno');
        // 下一步
        if (in_array($status, ['4', '5'])) {
            $this->next(16, $order_sn, $status == '4');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "leyu")]
    public function leyu(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('out_trade_no');
        // 下一步
        if (in_array($status, [2, 3])) {
            $this->next(17, $order_sn, $status == 2);
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "yuyanwang")]
    public function yuyanwang(Request $request)
    {
        $status = $request->input('nFlag');
        $order_sn = $request->input('szOrderId');
        // 下一步
        if (in_array($status, [2, 3])) {
            $this->next(18, $order_sn, $status == 2);
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "baimiao")]
    public function baimiao(Request $request)
    {
        $status = $request->input('resultcode');
        $order_sn = preg_replace('/\D/s', '', $request->input('userorderno'));
        // 下一步
        if (in_array($status, ['00000', '20017'])) {
            $this->next(19, $order_sn, $status == '00000');
        }
        // 返回
        return $this->response->json(['resultcode'=>'0','resultdescription'=>'succeed']);
    }

    #[Scene(scene: "jiumeng")]
    public function jiumeng(Request $request)
    {
        $status = $request->input('orderStatus');
        $order_sn = $request->input('orderNum');
        // 下一步
        if (in_array($status, [1, 2])) {
            $this->next(20, $order_sn, $status == 1);
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "leiniao")]
    public function leiniao(Request $request)
    {
        $status = $request->input('orderStatus');
        $order_sn = $request->input('merchantOrderNo');
        // 下一步
        if (in_array($status, [1, -1])) {
            $this->next(21, $order_sn, $status == 1);
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "xunyin")]
    public function xunyin(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('id');
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAIL'])) {
            $this->next(22, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        return $this->response->raw('OK');
    }

    #[Scene(scene: "lechong")]
    public function lechong(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('sellerid');
        // 下一步
        if (in_array($status, ['2', '4'])) {
            $this->next(23, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('OK');
    }

    #[Scene(scene: "fenxiaoe")]
    public function fenxiaoe(Request $request)
    {
        $order = ThirdPartyOrdersLogic::getInstance()->fenxiaoe($request->input('message'));
        if (empty($order)) {
            goto end;
        }
        // 剩余的
        $status = $order['charge_result'];
        $order_sn = $order['app_order_no'];
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAILED'])) {
            $this->next(24, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        end: return $this->response->json(['code' => '000000']);
    }

    #[Scene(scene: "shengshikeji")]
    public function shengshikeji(Request $request)
    {
        $status = $request->input('order_status');
        $order_sn = $request->input('trade_order_no');
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAILED'])) {
            $this->next(28, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "shengshikeji")]
    public function shengshikejibudaipiao(Request $request)
    {
        $status = $request->input('order_status');
        $order_sn = $request->input('trade_order_no');
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAILED'])) {
            $this->next(68, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "hengyixun")]
    public function hengyixun(Request $request)
    {
        $status = $request->input('State');
        $order_sn = $request->input('MOrderID');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(25, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "hengyixun")]
    public function hengyixun2(Request $request)
    {
        $status = $request->input('State');
        $order_sn = $request->input('MOrderID');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(29, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "xiyou")]
    public function xiyou(Request $request)
    {
        $status = $request->input('code');
        $order_sn = $request->input('OID');
        // 下一步
        if (in_array($status, ['1006', '1007'])) {
            $this->next(26, $order_sn, $status == '1006');
        }
        // 返回
        return $this->response->raw('yes');
    }

    #[Scene(scene: "yunbao")]
    public function yunbao(Request $request)
    {
        $status = $request->input('type');
        $order_sn = $request->input('order');
        // 下一步
        if (in_array($status, ['4', '5'])) {
            $this->next(27, $order_sn, $status == '4', 'mk_order');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "yunbao")]
    public function haotian(Request $request)
    {
        $status = $request->input('type');
        $order_sn = $request->input('order');
        // 下一步
        if (in_array($status, ['4', '5'])) {
            $this->next(41, $order_sn, $status == '4', 'mk_order');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "gongjiangrenxin")]
    public function gongjiangrenxin(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('other_order_no');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(66, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function yunfaka(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(71, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function mantukeji(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(74, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function jiangsuxiaoyuan(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(75, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function qingchenwangluo(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(72, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function fuhuikeji(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(79, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "weiyunka")]
    public function haotiankakayun(Request $request)
    {
        $status = $request->input('status');
        $order_sn = $request->input('orderno');
        // 下一步
        $this->next(80, $order_sn, $status == 5);
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "chengduleige")]
    public  function chengduleige(Request $request)
    {
        $status = $request->input('Status');
        $order_sn = $request->input('OutTradeNo');
        // 下一步
        if (in_array($status, ['2', '3'])) {
            $this->next(94, $order_sn, $status == '2');
        }
        // 返回
        return $this->response->raw('success');
    }

    #[Scene(scene: "xqy1")]
    public function xqy1(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('outer_order_id');
        // 下一步
        if (in_array($status, ['200', '500'])) {
            $this->next(99, $order_sn, $status == '200');
        }
        // 返回
        return $this->response->raw('success');
    }


    #[Scene(scene: "haiwei365")]
    public function haiwei365(Request $request)
    {
        $status = $request->input('state');
        $order_sn = $request->input('order_no');
        // 下一步
        if (in_array($status, ['SUCCESS', 'FAILURE'])) {
            $this->next(101, $order_sn, $status == 'SUCCESS');
        }
        // 返回
        return $this->response->raw('ok');
    }

    #[Scene(scene: "tiantianyuntong")]
    public function tiantianyuntong(Request $request)
    {
        $status = $request->input('ecode');
        $order_sn = $request->input('osn');
        // 下一步
        if (in_array($status, ['1002', '1003'])) {
            $this->next(102, $order_sn, $status == '1002');
        }
        // 返回
        return $this->response->raw('success');
    }














}
